﻿using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace ExampleIncrementalGenerator;

[Generator(LanguageNames.CSharp)]
public class CodeCollectionIncrementalGenerator : IIncrementalGenerator
{
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        var syntaxNodes = context.SyntaxProvider.CreateSyntaxProvider(
            (syntaxNode, cancellationToken) =>
            {
                return syntaxNode is ClassDeclarationSyntax classDeclaration && classDeclaration.Identifier.Text == "Hello";
            },
            (context, CancellationToken) => context.Node);

        context.RegisterSourceOutput(syntaxNodes, (sourceProductionContext, compilation) =>
        {
            var source = $@"
            namespace ConsoleApp1;

            public partial class Hello
            {{

                partial void HelloFrom(string name)
                {{
                    Console.WriteLine($""生成时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},Says: Hi from '{{name}}'"");
                }}
            }}
            ";

            sourceProductionContext.AddSource("Hello.g.cs", source);
        });

    }
}
